package com.web.base.open.util;

import com.blue.jdbc.Page;
import com.web.base.open.message.UrlRequest;
import com.web.base.open.message.UrlResponse;
import com.web.base.open.model.Url;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开放平台链接转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年02月27日
 */
public class UrlConvertor
{
	private UrlConvertor()
	{
	}
	
	public static Url toUrl(UrlRequest request)
	{
		if (request == null)
			return null;
		
		Url url = new Url();
		url.setId(request.getId());
		url.setParentId(request.getParentId());
		url.setName(request.getName());
		url.setDescp(request.getDescp());
		url.setUrl(request.getUrl());
		url.setState(request.getState());
		return url;
	}
	
	public static UrlResponse toResponse(Url url)
	{
		if (url == null)
			return null;
		
		UrlResponse response = new UrlResponse();
		response.setId(url.getId());
		response.setParentId(url.getParentId());
		response.setName(url.getName());
		response.setDescp(url.getDescp());
		response.setUrl(url.getUrl());
		response.setState(url.getState());

		response.setSelect(url.getSelect());
		return response;
	}
	
	public static List<UrlResponse> toResponse(List<Url> list)
	{
		List<UrlResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Url url : list)
		{
			responseList.add(toResponse(url));
		}
		return responseList;
	}
	
	public static Map<Integer, UrlResponse> toResponse(Map<Integer, Url> map)
	{
		Map<Integer, UrlResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, Url> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}
	
	public static Page<UrlResponse> toResponse(Page<Url> page)
	{
		if (page == null)
			return null;
		
		Page<UrlResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList()));
		return responsePage;
	}
	
}
